#ifndef _LCD_DRV_H_
#define _LCD_DRV_H_

#include "main.h"
#include <stdbool.h>

#define LCD_PORT CW_GPIOA
#define LCD_CLK RCC_AHB_PERIPH_GPIOA
#define LCD_LED_PIN GPIO_PIN_0
#define LCD_DATA_PIN GPIO_PIN_1
#define LCD_CS_PIN GPIO_PIN_4
#define LCD_WR_PIN GPIO_PIN_6

#define LCD_LED_SET() GPIO_WritePin(LCD_PORT, LCD_LED_PIN, GPIO_Pin_SET)
#define LCD_LED_RESET() GPIO_WritePin(LCD_PORT, LCD_LED_PIN, GPIO_Pin_RESET)

#define LCD_DATA_SET() GPIO_WritePin(LCD_PORT, LCD_DATA_PIN, GPIO_Pin_SET)
#define LCD_DATA_RESET() GPIO_WritePin(LCD_PORT, LCD_DATA_PIN, GPIO_Pin_RESET)

#define LCD_CS_SET() GPIO_WritePin(LCD_PORT, LCD_CS_PIN, GPIO_Pin_SET)
#define LCD_CS_RESET() GPIO_WritePin(LCD_PORT, LCD_CS_PIN, GPIO_Pin_RESET)

#define LCD_WR_SET() GPIO_WritePin(LCD_PORT, LCD_WR_PIN, GPIO_Pin_SET)
#define LCD_WR_RESET() GPIO_WritePin(LCD_PORT, LCD_WR_PIN, GPIO_Pin_RESET)

#define WRITE_CMD_CODE 0x80  // 发送命令
#define WRITE_DATA_CODE 0xA0 // 发送数据
#define READ_DATA_CODE 0xC0  // 读取数据

#define HT1621_SYS_EN 0x01  // 打开系统振荡器
#define HT1621_RC256 0x18   // 内部时钟
#define HT1621_BIAS 0x29    // 1/3duty 4com LCD偏置
#define HT1621_LCD_OFF 0x02 // 关闭LCD偏压
#define HT1621_LCD_ON 0x03  // 打开LCD偏压

#define HT1621_SYS_DIS 0x00  // 关闭系统振荡器和LCD偏压发生器
#define HT1621_XTAL 0x14     // 外部接时钟
#define HT1621_WDT_DIS 0x05  // 关闭看门狗
#define HT1621_TONE_ON 0x09  // 打开声音输出
#define HT1621_TONE_OFF 0x08 // 关闭声音输出

typedef struct Lcd
{
    uint8_t BAT_V[4];     // 电池电压
    uint8_t PV_V[4];      // 太阳能板电压，只有太阳能板有电才能检测到的电压
    uint8_t BAT_A[4];     // 电池电流
    uint8_t PV_A[4];      // 太阳能板电压
    uint8_t Temp[3];      // 温度
    uint8_t Lcd_DY_bit;   // 低压保护标志位 =1 是保护 =0是不保护
    uint8_t normalcy_Run; // 当板子不报错的时候，就可以正常显示电压电流值  =1 就出现报错，=0 正常显示电压电流
} BSP_Lcd_t;
extern BSP_Lcd_t bsp_lcd;

typedef enum
{
    USE,
    FLD,
    GEL,
    SLD,
    LI,
} BattType;

typedef enum
{
    twelveVolt,
    twentyFourVolt,
    thirtySixVolt,
    fortyEightVolt,
    seventyTwoVolt,
    ninetySixVolt,
    AUTO,
} BattVoltType;

void LcdDrvInit(void);
void LcdTest(void);
void WriteCmdToHT1621(uint8_t cmdVal);
void WriteDataToHT1621(uint8_t addr, uint8_t dataVal);
void WriteNDataToHT1621(uint8_t addr, uint8_t *dataVal, uint8_t cnt);
void LcdClearAll(void);

void PVVoltDisplay(void);
void PVCurrDisplay(void);
void BATVoltDisplay(void);
void BATCurrDisplay(void);
void LOADVoltDisplay(void);
void LOADCurrDisplay(void);
void TempDisplay(void);
void LcdScan(void);

#endif
